package com.kupurui.asteacher.ui.index;

import android.app.Activity;
import android.graphics.BitmapRegionDecoder;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.android.frame.util.AppJsonUtil;
import com.android.frame.util.Toolkit;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.adapter.MistakesRadioAdapter;
import com.kupurui.asteacher.adapter.ScreenRuleAdapter;
import com.kupurui.asteacher.bean.ChapterBean;
import com.kupurui.asteacher.bean.MistakesInfo;
import com.kupurui.asteacher.http.Teacher;
import com.kupurui.asteacher.ui.BaseAty;
import com.kupurui.asteacher.utils.UserManger;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.loadmore.LoadMoreContainer;
import in.srain.cube.views.ptr.loadmore.LoadMoreHandler;
import in.srain.cube.views.ptr.loadmore.LoadMoreListViewContainer;
import in.srain.cube.views.ptr.util.PtrInitHelper;
import okhttp3.Call;
import okhttp3.Response;

import static com.android.frame.application.BaseApplication.mContext;


/**
 * 错题本
 * Created by Administrator on 2017/7/21.
 */

public class MistakesAty extends BaseAty implements LoadMoreHandler, PtrHandler {


    @Bind(R.id.tv_bar_title)
    TextView tvBarTitle;
    @Bind(R.id.m_toolbar)
    Toolbar mToolbar;
    @Bind(R.id.radiobtn_mistakes1)
    RadioButton radiobtnMistakes1;
    @Bind(R.id.radiobtn_mistakes2)
    RadioButton radiobtnMistakes2;
    @Bind(R.id.radiobtn_mistakes3)
    RadioButton radiobtnMistakes3;
    @Bind(R.id.radiobtn_mistakes4)
    RadioButton radiobtnMistakes4;
    @Bind(R.id.radio_group)
    RadioGroup radioGroup;
    @Bind(R.id.listview)
    ListView listview;
    @Bind(R.id.tv_empty)
    TextView tvEmpty;
    @Bind(R.id.ll_empty)
    LinearLayout llEmpty;
    @Bind(R.id.load_more_list_view_container)
    LoadMoreListViewContainer loadMoreListViewContainer;
    @Bind(R.id.ptr_frame)
    PtrFrameLayout ptrFrame;
    @Bind(R.id.tv_num)
    TextView tvNum;

    List<MistakesInfo> radio_list;
    MistakesRadioAdapter mistakesRadioAdapter;

    PopupWindow popupWindow1;
    PopupWindow popupWindow2;
    PopupWindow popupWindow3;
    PopupWindow popupWindow4;

    private String sid = "";
    private String time = "";
    private String type = "";
    private String q_type = "";
    private String chapter_id = "";
    private int page = 1;
    List<ChapterBean> chapter_list;

    @Override
    public int getLayoutId() {
        return R.layout.mistakes_aty;
    }

    @Override
    public void initData() {
        if (getIntent().getExtras()!=null){
            sid = getIntent().getStringExtra("sid");
        }
        initToolbar(mToolbar,"错题本");
        chapter_list = new ArrayList<>();
        PtrInitHelper.initPtr(this, ptrFrame);
        ptrFrame.setPtrHandler(this);
        loadMoreListViewContainer.useDefaultFooter();
        loadMoreListViewContainer.setAutoLoadMore(true);
        loadMoreListViewContainer.setLoadMoreHandler(this);
        loadMoreListViewContainer.loadMoreFinish(false, true);
        listview.setFooterDividersEnabled(false);

        radio_list = new ArrayList<>();
        mistakesRadioAdapter = new MistakesRadioAdapter(this,radio_list,R.layout.mistakes_radio_item);
        listview.setAdapter(mistakesRadioAdapter);
        listview.setEmptyView(llEmpty);
    }

    @Override
    public void requestData() {
        showLoadingContent();
        new Teacher().mistake_ben(sid,time,type,q_type,chapter_id,page+"",this,0);
    }

    @OnClick({R.id.radiobtn_mistakes1,R.id.radiobtn_mistakes2,R.id.radiobtn_mistakes3,R.id.radiobtn_mistakes4})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.radiobtn_mistakes1:
                screen1();
                break;
            case R.id.radiobtn_mistakes2:
                screen2();
                break;
            case R.id.radiobtn_mistakes3:
                screen3();
                break;
            case R.id.radiobtn_mistakes4:
                showLoadingDialog("");
                new Teacher().choose_chap(sid,this,2);
                break;
        }
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        switch (what){
            case 0:
                page = 1;
                radio_list.clear();
                radio_list.addAll(AppJsonUtil.getArrayList(result,MistakesInfo.class));
                mistakesRadioAdapter.notifyDataSetChanged();
                tvNum.setText("筛选结果("+radio_list.size()+")");
                ptrFrame.refreshComplete();
                loadMoreListViewContainer.loadMoreFinish(false, true);
                break;
            case 1:
                if (Toolkit.listIsEmpty(AppJsonUtil.getArrayList(result,MistakesInfo.class))){
                    loadMoreListViewContainer.loadMoreFinish(false, false);
                }else{
                    page++;
                    radio_list.addAll(AppJsonUtil.getArrayList(result,MistakesInfo.class));
                    tvNum.setText("筛选结果("+radio_list.size()+")");
                    mistakesRadioAdapter.notifyDataSetChanged();
                    loadMoreListViewContainer.loadMoreFinish(false, true);
                }
                break;
            case 2:
                chapter_list.clear();
                chapter_list.addAll(AppJsonUtil.getArrayList(result,ChapterBean.class));
                screen4();
                break;
        }
        super.onSuccess(result, call, response, what);
    }

    private void screen1() {
        if (popupWindow1 == null) {
            popupWindow1 = new PopupWindow(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
            View view = LayoutInflater.from(this).inflate(R.layout.window_screen_rule_layout, null);
            popupWindow1.setContentView(view);
            View tvBack =  view.findViewById(R.id.tv_back);
            tvBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupWindow1.dismiss();
                }
            });
            ListView mlistView = (ListView) view.findViewById(R.id.listview);
            final List<String> list = new ArrayList<>();
            list.add("上次");
            list.add("上周");
            list.add("本月");
            list.add("全部");
            final ScreenRuleAdapter adapter = new ScreenRuleAdapter(this, list, R.layout.textview_layout);
            mlistView.setAdapter(adapter);
            popupWindow1.setBackgroundDrawable(new ColorDrawable());
            popupWindow1.setOutsideTouchable(true);

            mlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    adapter.setChooseIndex(i);
                    adapter.notifyDataSetChanged();
                    radiobtnMistakes1.setText(list.get(i));
                    popupWindow1.dismiss();
                    page = 1;
                    time = (i+1)+"";
                    new Teacher().mistake_ben(sid,time,type,q_type,chapter_id,page+"",MistakesAty.this,0);
                }
            });

            popupWindow1.showAsDropDown(radioGroup);
            popupWindow1.showAsDropDown(radioGroup);

        } else {
            popupWindow1.showAsDropDown(radioGroup);

        }

    }


    private void screen2() {
        if (popupWindow2 == null) {
            popupWindow2 = new PopupWindow(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
            View view = LayoutInflater.from(this).inflate(R.layout.window_screen_rule_layout, null);
            popupWindow2.setContentView(view);
            View tvBack = view.findViewById(R.id.tv_back);
            tvBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupWindow2.dismiss();
                }
            });
            ListView mlistView = (ListView) view.findViewById(R.id.listview);
            final List<String> list = new ArrayList<>();
            list.add("作业");
            list.add("在线考试");
            final ScreenRuleAdapter adapter = new ScreenRuleAdapter(this, list, R.layout.textview_layout);
            mlistView.setAdapter(adapter);
            popupWindow2.setBackgroundDrawable(new ColorDrawable());
            popupWindow2.setOutsideTouchable(true);

            mlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    adapter.setChooseIndex(i);
                    adapter.notifyDataSetChanged();
                    radiobtnMistakes2.setText(list.get(i));
                    popupWindow2.dismiss();
                    page = 1;
                    type = (i+1)+"";
                    new Teacher().mistake_ben(sid,time,type,q_type,chapter_id,page+"",MistakesAty.this,0);
                }
            });

            popupWindow2.showAsDropDown(radioGroup);
            popupWindow2.showAsDropDown(radioGroup);

        } else {
            popupWindow2.showAsDropDown(radioGroup);

        }

    }

    private void screen3() {
        if (popupWindow3 == null) {
            popupWindow3 = new PopupWindow(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
            View view = LayoutInflater.from(this).inflate(R.layout.window_screen_rule_layout, null);
            popupWindow3.setContentView(view);
            View tvBack = view.findViewById(R.id.tv_back);
            tvBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupWindow3.dismiss();
                }
            });
            ListView mlistView = (ListView) view.findViewById(R.id.listview);
            final List<String> list = new ArrayList<>();
            list.add("单选题");
            list.add("判断题");
            list.add("填空题");
            list.add("解答题");
            final ScreenRuleAdapter adapter = new ScreenRuleAdapter(this, list, R.layout.textview_layout);
            mlistView.setAdapter(adapter);
            popupWindow3.setBackgroundDrawable(new ColorDrawable());
            popupWindow3.setOutsideTouchable(true);

            mlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    adapter.setChooseIndex(i);
                    adapter.notifyDataSetChanged();
                    radiobtnMistakes3.setText(list.get(i));
                    popupWindow3.dismiss();
                    page = 1;
//                    if (i == 0){
//                        q_type = "3";
//                    }else if(i == 1){
//                        q_type = "1";
//                    }else if(i == 2){
//                        q_type = "4";
//                    }
                    q_type = (i+1)+"";
                    new Teacher().mistake_ben(sid,time,type,q_type,chapter_id,page+"",MistakesAty.this,0);
                }
            });

            popupWindow3.showAsDropDown(radioGroup);
            popupWindow3.showAsDropDown(radioGroup);

        } else {
            popupWindow3.showAsDropDown(radioGroup);

        }

    }


    private void screen4() {
        if (popupWindow4 == null) {
            popupWindow4 = new PopupWindow(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
            View view = LayoutInflater.from(this).inflate(R.layout.window_screen_rule_layout, null);
            popupWindow4.setContentView(view);
            View tvBack = view.findViewById(R.id.tv_back);
            tvBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupWindow4.dismiss();
                }
            });
            ListView mlistView = (ListView) view.findViewById(R.id.listview);
            final List<String> list = new ArrayList<>();
            for (int i = 0;i<chapter_list.size();i++){
                list.add(chapter_list.get(i).getTitle());
            }
            final ScreenRuleAdapter adapter = new ScreenRuleAdapter(this, list, R.layout.textview_layout);
            mlistView.setAdapter(adapter);
            popupWindow4.setBackgroundDrawable(new ColorDrawable());
            popupWindow4.setOutsideTouchable(true);

            mlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    adapter.setChooseIndex(i);
                    adapter.notifyDataSetChanged();
                    radiobtnMistakes4.setText(list.get(i));
                    popupWindow4.dismiss();
                    page = 1;
                    chapter_id = chapter_list.get(i).getId();
                    new Teacher().mistake_ben(sid,time,type,q_type,chapter_id,page+"",MistakesAty.this,0);
                }
            });
            popupWindow4.showAsDropDown(radioGroup);
            popupWindow4.showAsDropDown(radioGroup);
        } else {
            popupWindow4.showAsDropDown(radioGroup);
        }

    }

    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return PtrDefaultHandler.checkContentCanBePulledDown(ptrFrame, listview, header);
    }

    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {
        page = 1;
        new Teacher().mistake_ben(sid,time,type,q_type,chapter_id,page+"",this,0);
    }

    @Override
    public void onLoadMore(LoadMoreContainer loadMoreContainer) {
        new Teacher().mistake_ben(sid,time,type,q_type,chapter_id,(page+1)+"",this,1);
    }


}
